package net.linlan.config;


import net.linlan.plat.handler.ApiMemberInterceptor;
import net.linlan.plat.handler.ApiMemberLoginResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

/**
 * In order to deal with code questions create class to config webapp
 * Filename:WebAppConfig.java
 * Desc: WebApp config to deal with code questions
 * 提供给管理端进行使用的Web配置类
 * 提供给会员、API、前端进行使用的Web配置类
 *
 * @author Linlan
 * CreateTime:2017-10-15 11:36 AM
 *
 * @version 1.0
 * @since 1.0
 *
 */
@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converter.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain", Charset.forName("UTF-8"))));
        return converter;
    }
    //前台的配置
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(apiMemberInterceptor).addPathPatterns("/api/app/**");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(apiMemberLoginResolver);
    }

    //以下为后台的配置
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        本工程为独立api工程，不需要相关资源注入，上传后的附件需要直接映射，所以增加static目录
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    /**
     * 前台的配置，前台用户的正常处理
     */
    @Autowired
    private ApiMemberInterceptor apiMemberInterceptor;

    /**
     * 前台的配置，前台用户的登录
     */
    @Autowired
    private ApiMemberLoginResolver apiMemberLoginResolver;

}
